/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski   *
 *   g.latarowski@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


// Qt
#include <QListIterator>

// local
#include "roiitem.h"




// ===========================================================================
//                                   GROI
// ===========================================================================


// ------------------------------ GROI() -------------------------------------
GROI::GROI( QGraphicsPixmapItem * pPixmapItem )
    : QGraphicsItem( pPixmapItem ), _pPixmapItem( pPixmapItem )
{
//     setFlag( QGraphicsItem::ItemIsMovable );
    GROINode * pNode1 = new GROINode( this );
    GROINode * pNode2 = new GROINode( this );
    GROINode * pNode3 = new GROINode( this );
    GROINode * pNode4 = new GROINode( this );
    pNode1->setPos( 100, 100 );
    pNode2->setPos( 200, 100 );
    pNode3->setPos( 200, 200 );
    pNode4->setPos( 100, 200 );
    pNode1->setZValue( 1.1 );
    pNode2->setZValue( 1.1 );
    pNode3->setZValue( 1.1 );
    pNode4->setZValue( 1.1 );
    _nodeList << pNode1 << pNode2 << pNode3 << pNode4;
}
// ---------------------------------------------------------------------------


// ------------------------------ ~GROIItem() --------------------------------
GROI::~GROI()
{
}
// ---------------------------------------------------------------------------


// ------------------------- addChildToScene() -------------------------------
void GROI::addChildToScene()
{
    if( !scene() ) return;

    foreach( GROINode * pNode, _nodeList ) scene()->addItem( pNode );
}
// ---------------------------------------------------------------------------

        
// ---------------------------- boundingRect() -------------------------------
QRectF GROI::boundingRect() const
{
        // ROI bounding rect should be same as its pixmap parent 
    return _pPixmapItem->boundingRect();
}
// ---------------------------------------------------------------------------


// --------------------------- shape() ---------------------------------------
QPainterPath GROI::shape() const
{
    QPainterPath shape;
    shape.addRect( _pPixmapItem->boundingRect() );

    
    return shape;
}
// ---------------------------------------------------------------------------


// ----------------------------- paint() -------------------------------------
void GROI::paint( QPainter * pPainter,
                  const QStyleOptionGraphicsItem * pOption,
                  QWidget * widget )
{
    Q_UNUSED( pOption );
    Q_UNUSED( widget );
    
    pPainter->setPen( QPen( Qt::darkGreen, 2 ) );
    pPainter->setBrush( QColor( 0, 200, 0, 50 ) );

    pPainter->drawPolygon( _roi );
}
// ---------------------------------------------------------------------------


// --------------------------- adjust() --------------------------------------
void GROI::adjust()
{
        // clear previous ROI
    _roi.clear();

    removeFromIndex();
    
    QListIterator< GROINode * > iter( _nodeList );
    while( iter.hasNext() )
    {
        if( iter.peekNext() )
        {
            _roi << iter.next()->pos();
        }
        else
        {
            iter.next();
        }
    }

    addToIndex();

    update();
}
// ---------------------------------------------------------------------------






// ===========================================================================
//                                 GROINode
// ===========================================================================


// ------------------------------ GROINode() ---------------------------------
GROINode::GROINode( GROI * pROI )
    : _pROI( pROI )
{
    setFlags( QGraphicsItem::ItemIsMovable );
}
// ---------------------------------------------------------------------------


// ----------------------------- ~GROINode() ---------------------------------
GROINode::~GROINode()
{
}
// ---------------------------------------------------------------------------

    
// ------------------------- boundingRect() ----------------------------------
QRectF GROINode::boundingRect() const
{
    qreal adjust = 2;
    return QRectF( -10 - adjust, -10 - adjust,
                    23 + adjust,  23 + adjust );
}
// ---------------------------------------------------------------------------


// ------------------------------- shape() -----------------------------------
QPainterPath GROINode::shape() const
{
    QPainterPath path;
    path.addEllipse(-10, -10, 20, 20);
    return path;
}
// ---------------------------------------------------------------------------


// ------------------------------- paint() -----------------------------------
void GROINode::paint( QPainter * pPainter,
                      const QStyleOptionGraphicsItem * option,
                      QWidget * widget )
{
    Q_UNUSED( widget );
    
    pPainter->setPen(Qt::NoPen);
    pPainter->setBrush(Qt::darkGray);
    pPainter->drawEllipse(-7, -7, 20, 20);

    QRadialGradient gradient(-3, -3, 10);
    if (option->state & QStyle::State_Sunken) {
        gradient.setCenter(3, 3);
        gradient.setFocalPoint(3, 3);
        gradient.setColorAt(1, QColor(Qt::yellow).light(120));
        gradient.setColorAt(0, QColor(Qt::darkYellow).light(120));
    } else {
        gradient.setColorAt(0, Qt::yellow);
        gradient.setColorAt(1, Qt::darkYellow);
    }
    pPainter->setBrush(gradient);
    pPainter->setPen(QPen(Qt::black, 0));
    pPainter->drawEllipse(-10, -10, 20, 20);
}
// ---------------------------------------------------------------------------
    
    
// ----------------------------- mouseMoveEvent() ----------------------------
void GROINode::mouseMoveEvent( QGraphicsSceneMouseEvent * event )
{
    if( _pROI ) _pROI->adjust();

    QGraphicsItem::mouseMoveEvent( event );
}
// ---------------------------------------------------------------------------







// eof
